package br.com.dufs.mvc.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.dufs.mvc.dao.interfaces.OutroSemoventeDAO;
import br.com.dufs.mvc.model.OutroSemovente;

import com.fscoreunipe.mvc.dao.impl.generico.DaoGenericoImpl;

@Repository("outroSemoventeDAO")
public class OutroSemoventeDAOImpl extends
		DaoGenericoImpl<OutroSemovente, Integer> implements OutroSemoventeDAO {

	private Query query;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = false)
	@Override
	public List<OutroSemovente> listaPorCPF(String id) {
		// TODO Auto-generated method stub
		List<OutroSemovente> retorno = null;

		this.query = getSessionFactory().getCurrentSession().createQuery(
				"From OutroSemovente where  entrevistado='" + id + "'");

		if (this.query.list().size() > 0) {
			retorno = this.query.list();
		}
		return retorno;

	}

	@Transactional(readOnly = false)
	@Override
	public double quantidade(String id) {
		// TODO Auto-generated method stub

		double retorno = 0;

		this.query = getSessionFactory().getCurrentSession().createSQLQuery(
				"Select SUM(b.qnt) From OutroSemovente b where  entrevistado_cpf='"
						+ id + "'");

		if (this.query.uniqueResult() != null) {
			Double a = (Double) this.query.uniqueResult();

			retorno = a;
		}

		return retorno;
	}

	@Transactional(readOnly = false)
	@Override
	public double total(String id) {
		// TODO Auto-generated method stub
		double retorno = 0;

		this.query = getSessionFactory()
				.getCurrentSession()
				.createSQLQuery(
						"Select SUM(qnt * valorUniatrio) From OutroSemovente b where  entrevistado_cpf='"
								+ id + "'");

		if (this.query.uniqueResult() != null) {
			Double a = (Double) this.query.uniqueResult();
			System.out.println(a);
			retorno = a;
		}

		return retorno;
	}
}
